package marketplace_bankserver;


import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import marketplace_client.Client;
import marketplace_server.RejectedException;

public class BankClient 
{    
	Account account;
	Bank bankobj;
	private String bankname;
        private Client client;
	String clientname;

	
	public BankClient(String bankName) {
		this.bankname = bankName;
		try {
                    // Let's try to grab a reference to the bank, assuming that it's already registered!
                    bankobj = (Bank)Naming.lookup(bankname);
                    // If we found the bank in the RMI-registry we'll announce it to the world!
                    System.out.println("Bank lookup successfull!");
		} catch (Exception e1) {
                    
                    // If the lookup fails that probably means the bank hasn't been registered yet
                    // So we'll just try to register it and try to get that bankobj reference again :)
                    
                    try {registerBankService();} catch (Exception e2) {e2.printStackTrace();}
                    
		}
		System.out.println("Connected to bank: " + bankname);
	}
        public void  registerBankService () throws RemoteException 
        {
            // Since the registry is already setup in the ServerImpl.java
            // We can just grab the RMI service that's already running
            Registry r = LocateRegistry.getRegistry();
                try {
                    // Let's create a reference to that bankobj that we failed to establish earlier
                    bankobj = new BankImpl(BankImpl.class.getSimpleName());
                    // We'll now bind the bank to the RMI service
                    r.bind(BankImpl.class.getSimpleName(), bankobj);
                    // If all goes well we'll tell the world that our bank is running!
                    System.out.println("The bank is running");

                } catch (Exception ex) {}
        } 
	
        public Account getAccount () {
            return account;
        }

	public void executeCommand(Command command) throws RemoteException, RejectedException 
        {
		if (command == null) {
			return;
		}

		switch (command.getCommandName()) {
		case list:
			try {
				for (String accountHolder : bankobj.listAccounts()) {
					System.out.println(accountHolder);
				}
			} catch (Exception e) {
				return;
			}
			return;
		case quit:
			System.exit(1);
		case help:
			for (CommandName commandName : CommandName.values()) {
				System.out.println(commandName);
			}
			return;
		}

		// all further commands require a name to be specified
		String userName = command.getUserName();
		if (userName == null) {
			userName = clientname;
		}

		if (userName == null) {
			System.out.println("name is not specified");
			return;
		}

		switch (command.getCommandName()) {
		case newAccount:
			clientname = userName;
			bankobj.newAccount(userName);
			return;
		case deleteAccount:
			clientname = userName;
			bankobj.deleteAccount(userName);
			return;
		}

		// all further commands require a Account reference
		Account acc = bankobj.getAccount(userName);
		if (acc == null) {
			System.out.println("No account for" + userName);
			return;
		} else {
			account = acc;
			clientname = userName;
		}

		switch (command.getCommandName()) {
		case getAccount:
			System.out.println(account);
			break;
		case deposit:
			account.deposit(command.getAmount());
			break;
		case withdraw:
			account.withdraw(command.getAmount());
			break;
		case balance:
                    
			System.out.println("balance: $" + account.getBalance());
			break;
		default:
			System.out.println("Illegal command");
		}
	}
}
